﻿using System.Net.Sockets;
using System.Threading;
using Utility;

namespace _411Server
{
    class Comunicate
    {
        readonly Socket _s;
        Thread _thread;
        private byte[] _recvBytes;
        MyMessage _message;
        public Comunicate(Form1 form, Socket s)
        {
            _s = s;
            _recvBytes = new byte[1028];
        }

        public void SetThread(Thread thread)
        {
            _thread = thread;
        }


        public void Run()
        {
            try
            {
                while (true)
                {
                    _recvBytes = new byte[1024000];
                    _s.Receive(_recvBytes, _recvBytes.Length, 0);//从客户端接受信息
                    _message = (MyMessage)Serialize.Userialize(_recvBytes);
                    User friend = _message.Accepter;
                    Socket socket = (Socket)Manage.SocketIdTable[friend.Id];
                    socket.Send(_recvBytes);
                }
            }
            catch
            {
                _s.Close();
                if (_message != null)
                    Manage.Users.Remove(_message.Sender);
                _thread.Abort();
            }
        }
    }
}
